package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.common.pojo.PageResult;
import com.pinyougou.mapper.BrandMapper;
import com.pinyougou.pojo.Brand;
import com.pinyougou.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 品牌服务接口实现类
 *
 * @Author: shilv
 * @Date: Create in 8:41 2019/6/30
 * @Version: 1.0
 */
@Service //dubbo的注解:修饰的类为服务提供者
@Transactional
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public void save(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void delete(Serializable id) {

    }

    @Override
    public void deleteAll(Serializable[] ids) {

    }

    @Override
    public Brand findOne(Serializable id) {
        return null;
    }

    /** 查询所有的品牌 */
    @Override
    public List<Brand> findAll() {
        /*// 开始分页
        PageInfo<Brand> pageInfo = PageHelper.startPage(1, 5).doSelectPageInfo(new ISelect() {
            @Override
            public void doSelect() {
                brandMapper.selectAll();
            }
        });

        System.out.println("总记录数: " + pageInfo.getTotal());
        System.out.println("总页数: " + pageInfo.getPages());

        return pageInfo.getList();*/
        return brandMapper.selectAll();
    }
    /** 分页查询品牌 */
    @Override
    public PageResult findByPage(Brand brand, int page, int rows) {
        try {
            PageInfo<Brand> pageInfo = PageHelper.startPage(page, rows)
                    .doSelectPageInfo(new ISelect() {
                @Override
                public void doSelect() {
                    brandMapper.findAll(brand);
                }
            });
            return new PageResult(pageInfo.getPages(),pageInfo.getList());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    /** 查询全部品牌(id与name)*/
    @Override
    public List<Map<String, Object>> findAllByIdAndName() {
        try{
            /*List<Brand> brandList = brandMapper.selectAll();
            List<Map<String,Object>> data = new ArrayList<>();
            for (Brand brand : brandList) {
                Map<String,Object> map = new HashMap<>();
                map.put("id",brand.getId());
                map.put("text",brand.getName());
                data.add(map);
            }*/
            return brandMapper.findAllByIdAndName();
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }
}
